package com.example.online_forum.controller;

import com.example.online_forum.common.AppResult;
import com.example.online_forum.common.ResultCode;
import com.example.online_forum.exception.AppException;
import com.example.online_forum.model.Board;
import com.example.online_forum.services.IBoardService;
import com.example.online_forum.services.impl.BoardServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */

//日志注释
@Slf4j
@Api(tags = "板块接口")
@RestController
//路由映射
@RequestMapping("/board")
public class BoardController {
    @Autowired
    IBoardService boardService;
    @Value("${online_forum.index.board-num:9}")
    private Integer indexBoardNum;
    @ApiOperation("获取首页板块")
    @GetMapping("/topList")
    public AppResult<List<Board>> toplist(){
        log.info("首页板块个数: " + indexBoardNum);
        //调用service查询结果
        List<Board> boards = boardService.selectByNum(indexBoardNum);
        if(boards == null){
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }
    @ApiOperation("根据id获取板块信息")
    @GetMapping("/getById")
    public AppResult getById(@ApiParam("板块id")@RequestParam("id" ) @NonNull Long id){
        //service
        Board board = boardService.selectById(id);
        //板块已经删除或者无该论坛
        if(board == null || board.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + "getById");

            throw new AppException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //返回结果
        return AppResult.success(board);

    }
}
